package com.whatever.lottery.sfc;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

public class Bet {
	int money = 0;
	int beishu;
	String play;
	List<Item> itemList = new ArrayList<Item>();
	
	
	public int getMoney() {
		return money;
	}

	public int getBeishu() {
		return beishu;
	}
	
	public String getPlay() {
		return play;
	}

	public List<Item> getItemList() {
		return itemList;
	}

	public Bet(String input) throws Exception{
		String itemStr;
		
		//格式判断
		Matcher matcher = Constants.PATTERN.matcher(input);
		if (matcher.find()) {
			beishu = Integer.parseInt(matcher.group(1));
			itemStr = matcher.group(2);
		}else{
			throw new Exception("格式不正确!");
		}
		
		//倍数判断
		if (beishu<Constants.MIN_MULTI || beishu>Constants.MAX_MULTI){
			throw new Exception("倍数必须介于"+Constants.MIN_MULTI+"与"+Constants.MAX_MULTI+"之间!");
		}
		
		//投注串处理
		int sharpCount = 0;
		String[] arrItem = itemStr.split(",");
		
		
		for (String str: arrItem){
			Item item = new Item();
			int selectCount = 0;

			selectCount = str.length();
			
			try{
				for (char c:str.toCharArray()){
					item.addSelect(Select.fromValue(c));
					if (Select.fromValue(c).equals(Select.SFC9)){
						sharpCount +=1;
					}
				}
			}catch(Exception e){
				throw new Exception("选项不正确!");
			}
			
			
			
			if (selectCount != item.getSelects().size()) {
				throw new Exception("选项重复!");
			}
			
			itemList.add(item);
			
		}
		
		if (sharpCount > 0) {
			play = "R9";
			if (sharpCount != 5) {
				throw new Exception("场次数量不正确!");
			}
		}else{
			play = "SFC";
		}
		
		money=2;
		for (Item item:itemList){
			money *= item.getSelectCount();
		}
		
		money *= beishu;
		
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append(beishu).append("|");
		for (Item item:itemList){
			for (Select select:item.getSelects()){
				sb.append(select.toChar());
			}
			sb.append(',');
		}
		sb.setLength(sb.length()-1);
		
		return sb.toString();
	}
}
